﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using CefWebrtcDemo.wpf;

namespace CefWebrtcDemo
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            //winform
            //InitCefSettings();
            //CefForm cefForm = new CefForm(
            //    id: 0,
            //    title: "webrtc",
            //    initAddress: "https://webrtc.lssvc.cn",
            //    isLoadOnce: true
            //);
            //Application.Run(cefForm);

            //wpf
            var wpfThread = new Thread(() =>
            {
                InitCefSettings();
                var app = new System.Windows.Application();
                app.Run(new DemoWindow());
            });
            wpfThread.SetApartmentState(ApartmentState.STA);
            wpfThread.Start();
        }

        private static void InitCefSettings()
        {
#if ANYCPU
            //Only required for PlatformTarget of AnyCPU
            CefRuntime.SubscribeAnyCpuAssemblyResolver();
#endif
            //官方网站：https://cefsharp.github.io/
            
            CefSettings settings = new CefSettings();
            settings.Locale = "zh-CN";
            settings.AcceptLanguageList = "zh-CN";
            settings.WindowlessRenderingEnabled = true;

            //NOTE: WebRTC Device Id's aren't persisted as they are in Chrome see https://bitbucket.org/chromiumembedded/cef/issues/2064/persist-webrtc-deviceids-across-restart
            settings.CefCommandLineArgs.Add("enable-media-stream");
            //https://peter.sh/experiments/chromium-command-line-switches/#use-fake-ui-for-media-stream
            settings.CefCommandLineArgs.Add("use-fake-ui-for-media-stream");
            //For screen sharing add (see https://bitbucket.org/chromiumembedded/cef/issues/2582/allow-run-time-handling-of-media-access#comment-58677180)
            settings.CefCommandLineArgs.Add("enable-usermedia-screen-capturing");

            //---
            //https://peter.sh/experiments/chromium-command-line-switches
            //https://bitbucket.org/chromiumembedded/cef/wiki/BranchesAndBuilding
            //cef增强：窗口共享：https://github.com/epubcn/cef_enhancement/tree/master
            //cef doesn't currently support screen share : https://magpcss.org/ceforum/viewtopic.php?f=18&t=18715
            //---
            // settings.CefCommandLineArgs.Add("autoplay-policy", "no-user-gesture-required");
            // settings.CefCommandLineArgs.Add("no-proxy-server");
            // settings.CefCommandLineArgs.Add("enable-p2papi");
            // settings.CefCommandLineArgs.Add("enable-usermedia-screen-capturing");
            // settings.CefCommandLineArgs.Add("allow-file-access-from-files");
            // settings.CefCommandLineArgs.Add("allow-http-screen-capture");
            // settings.CefCommandLineArgs.Add("use-first-display-as-internal", "0");

            //webrtc音视频配置
            // settings.CefCommandLineArgs.Add("enable-media-stream", "1"); //启用后会导致接管PermissionHandler
            // settings.CefCommandLineArgs.Add("use-fake-ui-for-media-stream", "1"); //此项配置才能使音视频流真正传输，但会接管PermissionHandler
            // settings.CefCommandLineArgs.Add("use-fake-device-for-media-stream", "1"); //不能使摄像头和麦克风正常工作
            // settings.CefCommandLineArgs.Add("enable-system-flash", "1"); //（无用配置）flash
            // settings.CefCommandLineArgs.Add("enable-speech-input", "1"); //（无用配置）语音输入

            //渲染相关
            // settings.CefCommandLineArgs.Add("disable-gpu", "1"); //禁用GPU，解决部分电脑显示比例不正常的情况
            // settings.CefCommandLineArgs.Add("disable-gpu-compositing", "1");
            // settings.CefCommandLineArgs.Add("high-dpi-support", "1");
            // // settings.CefCommandLineArgs.Add("force-device-scale-factor", "" + GetWinScaling(cefForm));

            //共享相关
            // settings.CefCommandLineArgs.Add("enable-experimental-web-platform-features", "1"); //启用窗口共享等特性
            // settings.CefCommandLineArgs.Add("disable-tab-for-desktop-share", "0");
            // settings.CefCommandLineArgs.Add("auto-select-desktop-capture-source","display");
            // // settings.CefCommandLineArgs.Add("auto-select-desktop-capture-source", "整个屏幕");

            //网络相关
            // settings.CefCommandLineArgs.Add("--disable-web-security", "1"); //关闭同源策略,允许跨域
            // settings.CefCommandLineArgs.Add("--disable-site-isolation-trials", "1"); //关闭站点隔离策略,允许跨域
            // settings.CefCommandLineArgs.Add("--ignore-urlfetcher-cert-requests", "1"); //忽略证书验证
            // settings.CefCommandLineArgs.Add("--ignore-certificate-errors", "1"); //忽略证书验证
            // settings.CefCommandLineArgs.Add("--disable-features", "BlockInsecurePrivateNetworkRequests");
            // settings.CefCommandLineArgs.Add("allow-running-insecure-content", "1"); //通过此参数实现https页面访问http资源
            //
            Cef.Initialize(settings, true, (IBrowserProcessHandler)null);
            CefSharpSettings.WcfEnabled = true; //JS IPC
        }

        public static float GetWinScaling(Control control)
        {
            int dpiX;
            Graphics graphics = control.CreateGraphics();
            dpiX = (Int32)graphics.DpiX;
            // log("graphics.DpiX -> " + graphics.DpiX);
            if (dpiX == 96)
            {
                return 1;
            }
            else if (dpiX == 120)
            {
                return 1.25f;
            }
            else if (dpiX == 144)
            {
                return 1.5f;
            }
            else if (dpiX == 168)
            {
                return 1.75f;
            }
            else if (dpiX == 192)
            {
                return 2f;
            }
            else if (dpiX == 216)
            {
                return 2.25f;
            }
            else if (dpiX == 240)
            {
                return 2.5f;
            }
            else if (dpiX == 288)
            {
                return 3f;
            }
            else if (dpiX == 336)
            {
                return 3.5f;
            }
            else
            {
                return 1;
            }
        }
    }
}